
<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
    <th:block th:include="include :: header('部门信息')"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="white-bg">
<div class="container-div">
    <div class="ibox-content">
        <form class="form-horizontal m" id="deptForm" autocomplete="off">
            <input type="hidden" id="id" name="id" th:value="${id}">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">上级：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="hidden" name="parentId" id="parentId">
                    <input type="text" class="form-control" id="parentDept" checks="0" readonly textLabel="jasontext" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">单位名称：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" name="deptName" id="deptName" data-rule="required">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">负责人：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input class="form-control" type="hidden" name="leaderId" id="leaderId" readonly>
                        <input class="form-control" type="text" name="leader" id="leader" readonly>
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-danger" style="height: 31px;" onclick="clearUser();" >清除</button>
                            <button type="button" class="btn btn-primary" style="height: 31px;" onclick="selectUser();">选择</button>
                        </span>
                    </div>

                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">显示顺序：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="number" name="orderNum" value="1" id="orderNum" data-rule="required">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">联系电话：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" name="phone" id="phone">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">邮箱：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" name="email" id="email" data-rule="email;">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">部门类型：</label>
                <div class="col-sm-8">

                    <label class="check-box checkbox-success">
                        <input type="radio" class="icheck" id="deptType" name="deptType" value="0" />
                        分公司
                    </label>
                    <label class="check-box checkbox-success">
                        <input type="radio" class="icheck" id="deptType1"  name="deptType" value="1" checked=""/>
                        部门
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">

                    <label class="check-box checkbox-success">
                        <input type="radio" class="icheck" id="deptStatus" name="deptStatus" value="0" checked=""/>
                        启动
                    </label>
                    <label class="check-box checkbox-success">
                        <input type="radio" class="icheck" id="deptStatus1"  name="deptStatus" value="1"/>
                        停用
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input id="remark" name="remark" class="form-control" type="text">
                </div>
            </div>
            <div class="footerbar">
                <div class="col-sm-12 col-sm-offset-3">
                    <button class="btn btn-primary" type="submit">
                        <i class="fa fa-check"></i>
                        保存
                    </button>
                    <button class="btn btn-danger" type="button" onclick="cancel()">
                        <i class="fa fa-reply-all"></i>关闭
                    </button>
                </div>
            </div>
        </form>

    </div>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: ztree-js"/>
<!-- jquery-validate 表单树插件 -->
<script type="text/javascript" th:inline="javascript">
    $(function () {
        $('#deptForm').validator({
            stopOnError: false,
            timely: 2,
            theme: "yellow_right",
            valid: function (form) {
                //console.log(form);
                let parentIds=$('#parentDept_checkedIds').val();
                $('#parentId').val(parentIds);
                jutils.ajaxPost("/admin/dept/save",$(form).serialize(),function (res){
                    if(res.status){
                        cancel();
                    }
                });
            },
        });
        jutils.handleMessage(function (res) {
            //console.log(res);
            if(res.title=='userSelect'){
                $('#leader').val(res.data.userName);
                $('#leaderId').val(res.data.id);
            }

        });
        loadModel();
    });
    function loadModel(){
        let id=$('#id').val();

        jutils.ajaxGet('/admin/dept/getmodel',{id:id},function(res){
            if(res.status){
                if(!jutils.emptyId(id)){
                    let model=res.data.model;
                    $('#deptForm').initFormData(model);
                    $('#parentDept').attr('checks',model.parentId);
                }
                initSelectTree(res.data.ztreeData);
            }
        });
    }


    function selectUser(){
        let url='/admin/sysuser/userSelect';
        jutils.dialog('选择负责人',url);
    }
    function clearUser(){
        $('#leader').val('');
        $('#leaderId').val(0);
    }
    function initSelectTree(treeNodes) {
        let defaults = {
            callback: {
                onCheck: zTreeOnCheck
            },
            chkStyle: "radio",
            zNodes: treeNodes,
            height: 333   // 默认自定义下拉框高度
        }

       let tree= $("#parentDept").drawMultipleTree(defaults);

    }
    function zTreeOnCheck(treeNodes) {

    };
    function cancel(){
        jutils.closeDialog();
    }

</script>
</body>
</html>